package com.perye.dokit.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.perye.dokit.aop.log.Log;
import com.perye.dokit.base.BaseController;
import com.perye.dokit.entity.TestLi;
import com.perye.dokit.service.ChenTestService;
import com.perye.dokit.service.TestLiService;
import com.perye.dokit.query.TestLiQueryCriteria;
import com.perye.dokit.utils.ArrayAssembly;
import com.perye.dokit.utils.pageData.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Api(tags = "testLiBo管理")
@RestController
@RequestMapping("/api/testLi")
public class TestLiController extends BaseController {

    private final TestLiService testLiService;
    @Autowired
    private ChenTestService chenTestService;
    public TestLiController(TestLiService testLiService) {
        this.testLiService = testLiService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@dokit.check('testLi:list')")
    public void download(HttpServletResponse response, TestLiQueryCriteria criteria) throws IOException {
        testLiService.download(testLiService.queryAll(criteria), response);
    }

    @Log("查询testLiBo")
    @ApiOperation("查询testLiBo")
    @GetMapping()
    @PreAuthorize("@dokit.check('testLi:list')")
    public ResponseEntity<Object> getTestLis(){
//        return new ResponseEntity<>(testLiService.queryAll(criteria,pageable),HttpStatus.OK);
        PageData pageData1 = null;
        try {
            PageData pageData = this.getPageData();
            // PageData pageData = new PageData();
            //  PageHelper.startPage(pageable.getPageNumber()+1,  pageable.getPageSize());//页面的
            int a=Integer.parseInt(pageData.getString("page"))+1;
            PageHelper.startPage(a,Integer.parseInt(pageData.getString("size")));
            List<PageData> deptDtos = chenTestService.queryAllLi(pageData);

            PageInfo<PageData> pageInfo = new PageInfo<PageData>(deptDtos);
           int count=(int)pageInfo.getTotal();
            pageData1 = ArrayAssembly.pageList(deptDtos, count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(pageData1, HttpStatus.OK);
    }

    @Log("新增testLiBo")
    @ApiOperation("新增testLiBo")
    @PostMapping
    @PreAuthorize("@dokit.check('testLi:add')")
    public ResponseEntity<Object> create( @RequestBody TestLi resources){
        return new ResponseEntity<>(testLiService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改testLiBo")
    @ApiOperation("修改testLiBo")
    @PutMapping
    @PreAuthorize("@dokit.check('testLi:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TestLi resources){
        PageData pageData = this.getPageData();
        String name=pageData.getString("name");
    testLiService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除testLiBo")
    @ApiOperation("删除testLiBo")
    @PreAuthorize("@dokit.check('testLi:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) {
    testLiService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
